<?php

namespace App\Services;

use App\Common\Libs\ArrayHelper;
use App\Models\Field\ProField;
use App\Models\Field\UserField;
use App\Models\Pro;
use App\Models\Select\ProSelect;

/**
 * @package App\Services
 * @see ProService
 */
class ProService extends BaseService
{
    private $_repository;
    private $_pro;

    public function __construct()
    {
        parent::__construct();
        $this->_repository = repository()->ProRepository;
    }

    public function getProList($searchParams)
    {
        $dataProvider = $this->_repository->getProList($searchParams);
        $pagination = $this->getPagination();
        $pros = $dataProvider->get();

        // 用户
        $userIdArr = $pros->pluck('user_id')->all();
        $users = repository()->UserRepository->getUserByUserIdArr($userIdArr);
        $users = ArrayHelper::index($users, 'user_id');

        // 格式化结构
        $list = [];
        /**
         * @var Pro $pro
         */
        foreach ($pros as $pro) {
            $formatPro = $pro->commonFormat();
            $formatPro['user'] = isset($users[$pro->user_id]) ? UserField::format($users[$pro->user_id], UserField::getFormats()) : (object)[];
            $list[] = $formatPro;
        }

        return self::outputTotalCountList($dataProvider, $pagination, $list);
    }

    public function getRecommendProList($searchParams)
    {
        $dataProvider = $this->_repository->getRecommendProList($searchParams);
        $pagination = $this->getPagination(['page_size' => $searchParams['page_size']]);
        $pros = $dataProvider->get();

        // 不足page_size个，使用第一页的数据补齐
        if (count($pros) < $searchParams['page_size']) {
            $morePros = $this->_repository->getRecommendProList([
                'page' => 0,
                'page_size' => $searchParams['page_size'] - count($pros),
            ]);
            $pros = ArrayHelper::merge($pros, $morePros->get());
        }

        // 用户
        $userIdArr = ArrayHelper::getColumn($pros, 'user_id');
        $users = repository()->UserRepository->getUserByUserIdArr($userIdArr);
        $users = ArrayHelper::index($users, 'user_id');

        // 格式化结构
        $list = [];
        /**
         * @var Pro $pro
         */
        foreach ($pros as $pro) {
            $formatPro = ProField::format($pro, ProField::getFormats());
            $formatPro['user'] = isset($users[$pro['user_id']]) ? UserField::format($users[$pro['user_id']], UserField::getFormats()) : (object)[];
            $list[] = $formatPro;
        }

        return self::outputTotalCountList($dataProvider, $pagination, $list);
    }

    /**
     * 通过用户ID获取智囊团
     * @param $userId
     * @param $selectScene
     * @return \Illuminate\Database\Eloquent\Builder|Pro|object|bool
     */
    public function getProByUserId($userId, $selectScene = ProSelect::SELF_DETAIL)
    {
        if (empty($this->_pro[$userId])) {
            $pro = $this->_repository->getProByUserId($userId, $selectScene);
            if (!$pro) {
                return false;
            }
            $this->_pro[$userId] = $pro;
        }

        return $this->_pro[$userId];
    }
}
